from sqlalchemy import exc
from sqlalchemy.orm import Session

from crud import StudentAdminCrud
from model.TeachingModel import EmploymentModel
from schema.QuerySchema import QueryBase
from schema.TeachingSchema import EmploymentInfo
from util.ErrorCode import ErrorCode
from util.LogHelper import Log


def add_employment(employmentInfo: EmploymentInfo, db: Session):
    class_id = StudentAdminCrud.query_student_class_id_by_id(employmentInfo.student_id, db)
    if class_id == None:
        return {'code': ErrorCode.STUDENT_NOT_EXISTS.value, 'message': 'student not exists!'}
    else:
        employmentModel = EmploymentModel(class_id=class_id, student_id=employmentInfo.student_id, company_name=employmentInfo.company_name,
                                          work_location=employmentInfo.work_location, station=employmentInfo.station,
                                          entry_date=employmentInfo.entry_date, salary=employmentInfo.salary,
                                          offer=employmentInfo.offer, is_entry=employmentInfo.is_entry,
                                          remark=employmentInfo.remark)
        try:
            db.add(employmentModel)
            db.commit()
            return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
        except exc.SQLAlchemyError as e:
            db.rollback()
            Log.getlogger().error(f'add_employment failed:{employmentInfo} message: {e}')
            return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}

def mod_employment(employmentInfo: EmploymentInfo, db: Session):
    employmentModel = db.query(EmploymentModel).filter(EmploymentModel.id == employmentInfo.id).one_or_none()
    if employmentModel is not None:
        if employmentInfo.student_id != 0:
            employmentModel.student_id = employmentInfo.student_id
            class_id = StudentAdminCrud.query_student_class_id_by_id(employmentInfo.student_id, db)
            if class_id != None:
                employmentModel.class_id = class_id
            else:
                return {'code': ErrorCode.STUDENT_NOT_EXISTS.value, 'message': 'student not exists!'}
        if employmentInfo.company_name is not None:
            employmentModel.company_name = employmentInfo.company_name if len(
                employmentInfo.company_name) != 0 else employmentModel.company_name
        if employmentInfo.work_location is not None:
            employmentModel.work_location = employmentInfo.work_location if len(
                employmentInfo.work_location) != 0 else employmentModel.work_location
        if employmentInfo.station is not None:
            employmentModel.station = employmentInfo.station if len(
                employmentInfo.station) != 0 else employmentModel.station
        if employmentInfo.entry_date is not None:
            employmentModel.entry_date = employmentInfo.entry_date if len(
                employmentInfo.entry_date) != 0 else employmentModel.entry_date
        if employmentInfo.salary != 0:
            employmentModel.salary = employmentInfo.salary
        if employmentInfo.offer is not None:
            employmentModel.offer = employmentInfo.offer if len(employmentInfo.offer) != 0 else employmentModel.offer
        if employmentInfo.is_entry != 0:
            employmentModel.is_entry = employmentInfo.is_entry
        if employmentInfo.remark is not None:
            employmentModel.remark = employmentInfo.remark if len(
                employmentInfo.remark) != 0 else employmentModel.remark

        try:
            db.commit()
            return {'code': ErrorCode.SUCCESS.value, 'message': 'success'}
        except exc.SQLAlchemyError as e:
            Log.getlogger().error(f'mod_employment failed:{employmentInfo} message: {e}')
            return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}
    else:
        Log.getlogger().error(f'id:{employmentInfo.id} is not exists!')
        return {'code': ErrorCode.DATABASE_RECORD_NOT_FOUND.value, 'message': 'record not exists!'}


def query_employment_by_student_id(student_id: int, db: Session):
    try:
        result = db.query(EmploymentModel).filter(EmploymentModel.student_id == student_id).all()
        return {'code': ErrorCode.SUCCESS.value, 'message': 'success', 'data': result}
    except exc.SQLAlchemyError as e:
        Log.getlogger().error(
            f'query_employment_by_field failed! student_id: {student_id} message: {e}')
        return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}


def query_employment_by_page(query_base: QueryBase, db: Session):
    try:
        total_count = db.query(EmploymentModel).count()
        total_pages = (total_count + query_base.limit - 1) // query_base.limit

        result = db.query(EmploymentModel).offset((query_base.skip - 1) * query_base.limit).limit(query_base.limit).all()
        return {'code': ErrorCode.SUCCESS.value, 'message': 'success', 'total_count': total_count, 'total_pages': total_pages, 'data': result}
    except exc.SQLAlchemyError as e:
        Log.getlogger().error(f'query_employment_by_page failed! message: {e}')
        return {'code': ErrorCode.DATABASE_ERROR.value, 'message': 'database error'}
